﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AdventureWorks.Entities;
using AdventureWorksData.DAO;
using AdventureWorks.Business;
using System.Collections;
using System.Linq;

namespace AdventureWorks.View.Controllers
{ 
    public class AddressController : Controller
    {
		private readonly AddressBusiness _buzz;
		private readonly CustomerBusiness _customerBuzz;

		public AddressController()
		{
			_buzz = new AddressBusiness();
			_customerBuzz = new CustomerBusiness();
		}

		public AddressController(IUnitOfWork entityDb)
		{
			_buzz = new AddressBusiness(entityDb);
			_customerBuzz = new CustomerBusiness(entityDb);
		}

        public ViewResult Index(int? customerID)
        {
			ViewBag.CustomerID = customerID.GetValueOrDefault();

			var customers = _customerBuzz.GetList("Name", String.Empty);
			ViewBag.Customer = new SelectList(customers, "ID", "FullName", customerID);

			var customerFilter = customers.Where(c => !customerID.HasValue || c.ID == customerID.GetValueOrDefault());

			List<Address> ad = AddressFilter(customerFilter);

			return View(ad);
        }

		private static List<Address> AddressFilter(IEnumerable<Customer> customerFilter)
		{
			List<Address> ad = new List<Address>();
			foreach (var customer in customerFilter)
			{
				foreach (var address in customer.Addresses)
				{
					ad.Add(address);
				}
			}
			return ad;
		}

		public ViewResult Details(int id)
        {
            Address address = _buzz.Get(id);
            return View(address);
        }

		public ActionResult Create(int CustomerID)
        {
			ViewBag.Customer = _customerBuzz.Get(CustomerID);
            return View();
        } 

        [HttpPost]
		public ActionResult Create(Address address, int CustomerID)
        {
			if (ModelState.IsValid)
			{
				address = _buzz.Format(address, CustomerID);
				_buzz.Insert(address);
				_buzz.Save();

				return RedirectToAction("Index", new { Customer = CustomerID });
			}
			return View(address);
        }
        
		public ActionResult Edit(int id)
        {
			Address address = _buzz.Get(id);
            return View(address);
        }

        [HttpPost]
        public ActionResult Edit(Address address)
        {
			if (ModelState.IsValid)
			{
				address.ModifiedDate = DateTime.Now;
				_buzz.Update(address);
				_buzz.Save();


				var customerID = GetCustomerIDFromAddressID(address.ID);
				return RedirectToAction("Index", new { Customer = customerID });
			}
            return View(address);
        }

		public ActionResult Delete(int id)
        {
            Address address = _buzz.Get(id);
            return View(address);
        }

        [HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(int id)
        {
			var customerID = GetCustomerIDFromAddressID(id);

			_buzz.Delete(id);
			_buzz.Save();
			
			return RedirectToAction("Index", new { Customer = customerID });
        }

		private int GetCustomerIDFromAddressID(int addressID)
		{
			var address = _buzz.Get(addressID);
			var customer = address.Customers.FirstOrDefault();
			return customer.ID;
		}

        protected override void Dispose(bool disposing)
        {
            _buzz.Dispose();
            base.Dispose(disposing);
        }
    }
}